package com.kili.cloud.shorturl.service.impl;

import com.kili.cloud.common.util.BeanUtils;
import com.kili.cloud.shorturl.repository.entity.UrlMapInfo;
import com.kili.cloud.shorturl.service.model.UrlMapInfoModel;
import com.kili.cloud.shorturl.repository.UrlMapInfoDao;
import com.kili.cloud.shorturl.service.UrlMapInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: url映射服务实现类
 * @author: tom.liu
 * @date: 2019/6/16 21:34
 */
@Service
public class UrlMapInfoServiceImpl implements UrlMapInfoService {
    private static Logger logger = LoggerFactory.getLogger(UrlMapInfoServiceImpl.class);

    @Autowired
    UrlMapInfoDao urlMapInfoDao;

    @Override
    public long insertSelective(UrlMapInfoModel urlMapInfoModel) {
        UrlMapInfo urlMapInfo = new UrlMapInfo();
        BeanUtils.copyProperties(urlMapInfoModel, urlMapInfo);
        urlMapInfoDao.insertSelective(urlMapInfo);
        return urlMapInfo.getId();
    }

    @Override
    public int updateByPrimaryKeySelective(UrlMapInfoModel urlMapInfoModel) {
        UrlMapInfo urlMapInfo = new UrlMapInfo();
        BeanUtils.copyProperties(urlMapInfoModel, urlMapInfo);
        return urlMapInfoDao.updateByPrimaryKeySelective(urlMapInfo);
    }

    @Override
    public List<UrlMapInfoModel> selectBySelective(UrlMapInfoModel urlMapInfoModel) {
        UrlMapInfo urlMapInfo = new UrlMapInfo();
        BeanUtils.copyProperties(urlMapInfoModel, urlMapInfo);
        List<UrlMapInfo>  urlMapInfoList = urlMapInfoDao.selectBySelective(urlMapInfo);
        List urlMapInfoModelList = new ArrayList<UrlMapInfoModel>();
        BeanUtils.copyProperties(urlMapInfoList, urlMapInfoModelList, UrlMapInfo.class, UrlMapInfoModel.class);
        return  urlMapInfoModelList;
    }

    @Override
    public UrlMapInfoModel selectOriginalUrlByUri(String uri) {
        UrlMapInfo urlMapInfo = urlMapInfoDao.selectOriginalUrlByUri(uri);
        UrlMapInfoModel urlMapInfoModel = new UrlMapInfoModel();
        if(urlMapInfo == null){
            urlMapInfoModel = null;
        }else {
            BeanUtils.copyProperties(urlMapInfo, urlMapInfoModel);
        }
        return urlMapInfoModel;
    }

    @Override
    public int updateVisitCount(Long id){
        return  urlMapInfoDao.updateVisitCount(id);
    }

}
